package com.itheima.service.impl;

import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    //批量导入预约数据
    @Override
    public void importOrderSettings(List<OrderSetting> orderSettingList) {


        for (OrderSetting orderSetting : orderSettingList) {
            //根据日期判断当前的预约是否存在，已存在只更新预约数量，不存在添加预约数据
            Integer num = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
            if (num > 0) { //有数据，只更新
                orderSettingMapper.updateNumberByOrderDate(orderSetting.getOrderDate());
            } else {
                orderSettingMapper.addOrderSetting(orderSetting);
            }


        }
    }


    //获取预约数据
    @Override
    public List<Map> findOrderSettingByDate(Integer year, Integer month) {
        //将年和月进行拼接，方便数据库的查询
        String beginDate=year+"-"+month+"-1";
        String endDate=year+"-"+month+"-31";
        List listMap=new ArrayList();
        List<OrderSetting> orderSettingList=orderSettingMapper.findOrderSettingByDate(beginDate,endDate);
        for (OrderSetting orderSetting : orderSettingList) {
            Map map=new HashMap();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            listMap.add(map);
        }
        return listMap;
    }


    //更新预约数量
    @Override
    public void updateOrderSettingData(String dayInfo, Integer value) {
        orderSettingMapper.updateOrderSettingData(dayInfo,value);
    }

}
